﻿import os
import sys
import shutil
import time

proj_dirs = [".", "../library", "../main", "../passwordlibrary", "../PullToRefreshAndLoad", "../ViewPagerIndicator_Library"]
unsigned_package = "./bin/SplashScreenActivity-release-unsigned.apk"
signed_package = "./bin/SplashScreenActivity-release-signed.apk"
release_package = "HomeActivity"
test_package = "HomeActivity_test"
env_file = "./src/com/kkd/kkdapp/Environment.java"

arrChannel = [ "yingyongbao_01"         #应用宝
           ,"wandoujia_02"              #豌豆荚
           ,"360zhushou_03"             #360助手
           ,"baiduzhushou_04"           #百度助手
           ,"xiaomi_05"                 #小米
           ,"huawei_06"                 #华为
           ,"meizu_07"                  #魅族
           ,"anzhi_08"                  #安智
           ,"ppzhushou_10"              #PP助手
           ,"fuwuduan_11"               #服务端
           ,"shougouzhushou_12"         #搜狗助手
           ,"360tuiguang_02"            #360推广
           ,"xinlangfuyituiguang_05"    #新浪扶翼推广
           ,"weixintuiguang_07"         #微信推广
           ,"weibotuiguang_08"          #微博推广
           ,"tiebatuiguang_09"          #贴吧推广
           ,"jinritoutiaotuiguang_11"   #今日头条推广
           ,"huisuopingtuiguang_13"     #惠锁屏推广
           ,"oppoyingyongshangdian_17"  #oppo应用商店
           ,"yingyonghui_18"            #应用汇
           ,"jifeng_19"                 #机锋
           ,"nduo_20"                   #N多
           ,"youyi_21"                  #优亿
           ,"mumayi_22"                 #木蚂蚁
           ,"mmshangcheng_23"           #MM商城
           ,"woshangcheng_24"           #沃商城
           ,"tianyikongjian_25"         #天翼空间
           ,"leshangdian_26"            #乐商店
           ,"wifiwannengyaoshi_27"      #wifi万能钥匙
           ,"baidum_28"                 #百度M端落地页
           ,"xinlangweicaifu_29"        #新浪微财富
           ,"baitong_29"                #百通
           ,"91zhushou_31"              #91助手
           ,"anzhuomarket_32"           #安卓市场
           ,"jingrui_01_33"             #精瑞1
           ,"jingrui_02_33"             #精瑞2
           ,"feipao1_34"                #飞跑1
           ,"feipao2_34"                #飞跑2
           ,"feipao3_34"                #飞跑3
           ,"liangwang1_35"             #亮网1
           ,"liangwang2_35"             #亮网2
           ,"liqu_36"                   #历趣
           ,"VIVO_37"                   #VIVO
           ,"ppzhushoufufei_38"         #PP助手付费
           ,"ppzhushoufufei1_38"        #pp助手付费1
           ,"ppzhushoufufei2_38"        #pp助手付费2
           ,"ppzhushoufufei3_38"        #pp助手付费3
           ]

def getCurDate():
    t = time.localtime()
    strDate = str(t.tm_year)

    if t.tm_mon < 10:
        strDate += "0"
    strDate += str(t.tm_mon)

    if t.tm_mday < 10:
        strDate += "0"
    strDate += str(t.tm_mday)

    return strDate

def getCurDateTime():
    t = time.localtime()
    strDate = str(t.tm_year)

    if t.tm_mon < 10:
        strDate += "0"
    strDate += str(t.tm_mon)

    if t.tm_mday < 10:
        strDate += "0"
    strDate += str(t.tm_mday)

    if t.tm_hour < 10:
        strDate += "0"
    strDate += str(t.tm_hour)

    if t.tm_min < 10:
        strDate += "0"
    strDate += str(t.tm_min)

    if t.tm_sec < 10:
        strDate += "0"
    strDate += str(t.tm_sec)

    return strDate

strDate = getCurDate()
strTime = getCurDateTime()

def getSdkDir():
    strDir = ""
    if len(sys.argv) > 1:
        strDir = sys.argv[1]
        strDir = strDir.replace( "/", "\\" )
    else:
        strDir = os.environ["ANDROID_HOME"];
    return strDir.replace( "\\", "\\\\" )

def genLocalProp():
    f = open( "local.properties", "w" )
    f.write( "sdk.dir=" + getSdkDir() );
    f.close()

def doSign():
    strCmd = "jarsigner -sigalg MD5WithRSA "
    strCmd += "-digestalg SHA1 "
    strCmd += "-keystore ./android.keystore "
    strCmd += "-storepass kkd123 "
    strCmd += "-signedjar "
    strCmd += signed_package + " "
    strCmd += unsigned_package + " kkd"
    os.system( strCmd )
    print( strCmd + " finished" )

def doZipAlign(dst):
    strDst = "./package/" + strDate + "/" + strTime + "/" + dst + ".apk"
    strCommonDst = "./package/" + dst + ".apk"
    strCmd = "zipalign -f -v 4 "
    strCmd += signed_package + " " + strDst
    os.system( strCmd )
    print( strCmd + " finished" )
    shutil.copy( strDst, strCommonDst )

def doBuild():
    os.system( "ant clean" )

    for strName in arrChannel:
        shutil.copy( "AndroidManifestBase.xml", "AndroidManifest.xml" )
        strCmd = "sed -i -e \"s/CHANNEL_NAME/" + strName + "/g\" ./AndroidManifest.xml"
        os.system( strCmd );
        print( strCmd )

        strCmd = "sed -i -e \"s/IS_DEBUG = false/IS_DEBUG = true/g\" " + env_file
        os.system( strCmd )
        print( strCmd )

        strCmd = "ant -f build2.xml release"
        os.system(strCmd)
        doSign()
        doZipAlign(release_package + "_test_" + strName)

        strCmd = "sed -i -e \"s/IS_DEBUG = true/IS_DEBUG = false/g\" " + env_file
        os.system( strCmd )
        print( strCmd )

        strCmd = "ant -f build2.xml release"
        os.system(strCmd)
        doSign()
        doZipAlign(release_package + "_" + strName)


def createDir():
    cur_dir = os.path.dirname(os.path.realpath(__file__))
    if not os.path.exists( "./package" ):
        os.mkdir( "./package" )
    if not os.path.exists( "./package/" + strDate ):
        os.mkdir( "./package/" + strDate )
    if not os.path.exists( "./package/" + strDate + "/" + strTime ):
        os.mkdir( "./package/" + strDate + "/" + strTime )



def main():  
    genLocalProp()
    createDir()
    doBuild()
main()